public class demo1 {
    class Solution {
        int []dx={0,0,1,-1,-1,-1,1,1};
        int []dy={1,-1,0,0,-1,1,-1,1};
        int m,n;
        public char[][] updateBoard(char[][] board, int[] click) {
            int i=click[0];
            int j=click[1];
            if(board[i][j]=='M'){
                board[i][j]='X';
                return board;
            }
            m=board.length;
            n=board[0].length;
            dfs(board,i,j);
            return board;
        }
        public void dfs(char[][]board,int i,int j){
            //统计周围地雷的数量
            int count=0;
            for(int k=0;k<8;k++){
                int a=i+dx[k];
                int b=j+dy[k];
                if(a>=0&&a<m&&b>=0&&b<n&&board[a][b]=='M'){
                    count++;
                }
            }
            if(count==0){
                board[i][j]='B';
                for(int k=0;k<8;k++){
                    int a=i+dx[k];
                    int b=j+dy[k];
                    if(a>=0&&a<m&&b>=0&&b<n&&board[a][b]=='E'){
                        dfs(board,a,b);
                    }
                }
            }else{
                board[i][j]=(char)(count+'0');
                return;
            }
        }
    }
}
